//获取应用实例
var app = getApp();
var util = require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: true,//指示点
    autoplay: true,//自动切换
    interval: 5000,//切换间隔
    duration: 1000,//动画时长
    page:1,//分页
    lock:false,
    loading:true
    // id:1,//1：侨益前端，2：司机端
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        that.setData({
          nickName: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl,
        })
      },
    })
    app.getLoginRole(function (role) {
      that.setData({
        role: role
        // role:'admin'
      })
      if(that.data.role=='user'){
        that.getpallet();
        app.getLoginSession(function (session) {
          wx.request({

            // 请求地址
            url: app.api + "/wx_log_test/msg/broadcast",

            // 请求参数
            data: {
              token: session,
              page: 1,
              perpage: 10,
              isread:0
            },

            // 请求成功
            // 请求成功
            success: function (res) {
              var res = res.data;
              if (res.msg == "OK") {
                var data = res.data;
                that.setData({
                  newsnum:data.broadcastPage.content.length
                })
              }
            }
          })
        })
      }
      else{
        app.getLoginSession(function (session) {
          wx.request({

            // 请求地址
            url: app.api + "/wx_log_test/msg/sys",

            // 请求参数
            data: {
              token: session,
              page: 1,
              perpage: 10,
              isread:0
            },

            // 请求成功
            // 请求成功
            success: function (res) {
              var res = res.data;
              if (res.msg == "OK") {
                var data = res.data;
                that.setData({
                  newsnum: data.broadcastPage.content.length
                })
              }
            }
          })
        })
        that.getdriver();
      }
    })
    that.banner();
  },
  //获取轮播图
  banner:function(){
    var that = this;
    wx.request({
      url: app.api +'/wx_log_test/app/carousel',
      success:function(res){
        var res = res.data;
        if(res.msg=="OK"){
          that.setData({
            banner:res.data
          })
        }
      }

    })
  },
  //获得货盘
  getpallet:function(res){
    var that = this;
    var page = that.data.page;
    // 加锁
    if (that.data.lock) return;
    that.setData({
      lock: true
    });
    app.getLoginSession(function(session){
      wx.request({
        url: app.api + '/wx_log_test/cargo/list',
        data: {
          token:session,
          page:1,
          perpage:4
        },
        success:function(e){
          console.log(e);
          var res = e.data;
          if(res.msg=="OK"){
            that.setData({
              pallet: that.deallist(res.data.content),
              loading:false
            })
          }else{
            that.setData({
              loading: false,
              errorTips: "接口请求失败" + e.statusCode
            })
          }
        },
        fail:function(e){
          that.setData({
            loading:true,
            errorTips: "接口请求失败" + e.statusCode
          })
        }
      })
    })
    
  },
  //获得司机推荐
  getdriver: function (res) {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/sys/approve',
        data: {
          token: session,
          page: 1,
          perpage: 4,
          status: 1
        },
        success: function (res) {
          console.log(res);
          var res = res.data;
          if (res.msg == "OK") {
            var data = res.data;
            if (data.content.length == 0) {
              that.setData({
                loading: false,
                loadtips: '暂无相关数据',
                content: []
              })
            } else {
              that.setData({
                loading: false,
                drivers: res.data.content,
              });
              console.log("drivers", res.data.content)
            }
          } else {
            that.setData({
              loading: false,
              loadtips: res.msg
            })
          }
        },
        fail: function (res) {
          that.setData({
            loading: false,
            loadtips: '接口请求失败'
          })
        }
      })
    })
  },
  // 上拉触底
  lower: function () {
    var that = this;
    that.getData();
  },
  call: function (res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.num //仅为示例，并非真实的电话号码
    })
  },
  onShareAppMessage: function () {

  },
  deallist(list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        console.log(item);
        item.send_date1 = util.formatDate(new Date(item.send_date1), 'YYYY-MM-dd')
      })
      return list
    }
  },
})